<template>
  <!-- 逾期记录组件  v-loading.fullscreen.lock="fullscreenLoading" -->
  <div class="ContactPerson">
    <div class="searchNav">
      <!-- <h2>联系人</h2> -->
      <!-- <el-button
        class="button"
        icon="el-icon-plus"
        type="primary"
        style="margin-left: 0"
        @click="handleAdd"
        >新增</el-button
      > -->
      <div class="searchParams">
        <div class="block">
          <el-date-picker
            v-model="range"
            type="daterange"
            style="width: 250px; margin-left: 10px"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            @change="changeTime"
          >
          </el-date-picker>
        </div>
        <!-- <el-date-picker
            v-model="queryParam.LoanTime"
            value-format="yyyy-MM-dd"
            type="date"
            placeholder="选择">赊货日期
        </el-date-picker> -->
        <el-input
          class="input"
          v-model="queryParam.Keyword"
          placeholder="请输入赊货方/销售方"
          clearable
          @keydown.enter.native="getDataList"
        ></el-input>
        <el-button class="button" @click="search" type="primary"
          >查询</el-button
        >
        <el-button class="button" @click="reset('reset')">重置</el-button>
      </div>
    </div>
    <div>
      <el-table :data="tableData" style="width: 100%" :max-height="height">
        <el-table-column prop="BuyerName" label="赊货方">
          <template slot-scope="scope">
            <el-link @click="handlelDetail(scope.row)">{{
              scope.row.BuyerName
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="CompanyName" label="销售方"> </el-table-column>
        <el-table-column prop="AccountingStartTime" label="赊货日期">
          <template slot-scope="scope">
            <span>{{
              scope.row.AccountingStartTime
                ? "&nbsp;" + scope.row.AccountingStartTime.substring(0, 10)
                : ""
            }}</span>
            <!-- <span>{{
			  scope.row.AccountingEndTime
			    ? "&nbsp;" + scope.row.AccountingEndTime.substring(0, 10)
			    : ""
			}}</span> -->
          </template>
        </el-table-column>
        <el-table-column prop="Period" label="账期">
          <template slot-scope="scope">
            <span>{{ scope.row.Period + "天" }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="Days" label="逾期天数">
          <template slot-scope="scope">
            <span>{{
              scope.row.Days ? scope.row.Days + " 天" : 0 + " 天"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="ReceiveSum" label="逾期金额">
          <template slot-scope="scope">
            <span>{{ toThousands(scope.row.TotalSum) }}</span>
          </template>
        </el-table-column>

        <!-- <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleDelete(scope.row)"
              style="color: red"
              >删除</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>
      <!-- 分页 -->
      <div style="display: flex; justify-content: flex-end; margin-top: 8px">
        <pagination
          :total="pagination.total"
          :page.sync="pagination.PageIndex"
          :limit.sync="pagination.PageRows"
          @pagination="handlePageChange"
        />
      </div>
    </div>
    <EditForm ref="editForm" :parentObj="{ ...this }" />

    <!-- 资质详情 -->
    <MyMerchantDetail ref="MyMerchantDetail" :parentObj="{ ...this }" />
  </div>
</template>

<script>
import radioGroup from "@/components/CompanyRadioGroup.vue";
import { toThousands } from "@/utils/Tools";
import EditForm from "./PastDueRecordsForm.vue";
// 引入资质详情组件 统一用的是我的商户页面里的资质详情
import MyMerchantDetail from "../MyMerchant/MyMerchantDetail.vue";
export default {
  props: {},
  components: {
    EditForm,
    radioGroup,
    MyMerchantDetail,
  },
  mounted() {
    // 获取预期记录列表数据
    this.getDataList();
  },
  created() {},
  computed: {
    height() {
      return document.documentElement.clientHeight * 0.75;
    },
  },
  data() {
    return {
      // 整体页面加载
      fullscreenLoading: false,
      // 时间范围
      range: [],
      pagination: {
        current: 1,
        pageSize: 25,
        total: 0,
        pageSizeOptions: [25, 50, 100],
      },
      sorter: {
        field: "CreateTime",
        order: "desc",
      },
      // 时间范围
      range: [],
      tableData: [],
      queryParam: {
        Keyword: "",
        StartDate: "",
        EndDate: "",
      },
      loading: false,
      toThousands,
    };
  },
  methods: {
    // 商铺资质详情跳转
    handlelDetail(row) {
      console.log("资质详情", row);
      if (row.BuyerStoreId) {
        this.$refs.MyMerchantDetail.openForm(row.BuyerStoreId);
      } else {
        this.$message.error("没有店铺信息");
      }
    },
    // 开始结束时间选择
    changeTime(e) {
      this.range = e;
      console.log("开始时间", e, this.queryParam);
      this.queryParam.StartDate = this.range[0];
      this.queryParam.EndDate = this.range[1];
      // 刷新放款列表
      this.tableData = [];
      this.pagination.current = 1;
      this.getDataList();
    },
    // 参数重置
    reset(event) {
      if (event === "reset") {
        this.range = [];
        this.queryParam.Keyword = "";
        this.range = [];
        this.queryParam.StartDate = "";
        this.queryParam.EndDate = "";
      }
      this.tableData = [];
      this.pagination.current = 1;
      this.getDataList();
    },
    // 搜索
    search() {
      this.reset();
    },
    // 新增
    handleAdd() {
      this.$refs.editForm.openForm();
    },
    // 删除
    handleDelete(row) {
      this.$message.success("已删除");
    },
    // 分页函数
    handlePageChange(data) {
      this.pagination.current = data.pageIndex;
      this.pagination.pageSize = data.pageSize;
      this.getDataList(); //这是重新获取修改日志列表
    },
    // 获取逾期列表数据
    getDataList() {
      let userId = JSON.parse(sessionStorage.getItem("userInfo"));
      this.$http
        .post(`/Base/Bank_Info/Getuserids?Id=${[userId.Id]}`)
        .then((res) => {
          this.userIdList = res.Data;
          if (res.Success) {
            this.getPastDuerecords(res.Data);
          }
        });
    },
    getPastDuerecords(data) {
      var that = this;
      //创建loading对象开始遮罩
      this.loading = true;
      this.$http
        .post("/Base/Bank_Info/PastDuerecords", {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || "Id",
          SortType: this.sorter.order,
          Search: {
            userids: data,
            Keyword: this.queryParam.Keyword,
            StartDate: this.queryParam.StartDate,
            EndDate: this.queryParam.EndDate,
          },
        })
        .then((resJson) => {
          //请求结束关闭loading
          setTimeout(function () {
            that.loading = false;
          }, 600);
          if (resJson.Success) {
            this.tableData = resJson.Data;
            if (this.tableData) {
              const pagination = { ...this.pagination };
              pagination.total = resJson.Total;
              this.pagination = pagination;
            }
          }
        });
    },
  },
  watch: {},
};
</script>

<style lang="less" scoped></style>
